﻿#region License statement
// NJamb, a specification and delta-specification DSL
// Copyright (c) 2010-2011, Mark Knell
// Published under the MIT License; all other rights reserved
#endregion

#region using...
using System;
using System.Collections.Generic;
using NJamb.Specifications;
using Stile.Patterns.SelfDescribingPredicates;
using Stile.Types.Reflection;
#endregion

namespace NJamb.Parser.SpecificationBuilders
{
    public static class SpecificationBuilderExtensions
    {
        public static TSpecifies Contains<TSubject, TItem, TSpecifies>(
            this ISpecificationBuilder<TSubject, TSpecifies> specificationBuilder, TItem item)
            where TSubject : class, IEnumerable<TItem> where TSpecifies : class, ISpecification
        {
            return specificationBuilder.Satisfies(ExpectEnumerable<TSubject, TItem>.ToContain(item, Negated.False));
        }

        public static Func<Exception, TSubject, IEvaluation<TSubject>> MakeExceptionFilter<TSubject, TException>(
            Lazy<string> description) where TException : Exception
        {
            return (e, subject) =>
                   {
                       if (e is TException)
                       {
                           return new Evaluation<TSubject>(subject);
                       }
                       string but;
                       if (e == null)
                       {
                           but = " but no exception was thrown.";
                       }
                       else
                       {
                           but = " but threw " + e.GetType().ToDebugString() + ".";
                       }

                       return
                           new Evaluation<TSubject>(
                               "expected " + description.Value + " would throw " + typeof(TException).ToDebugString()
                               + Environment.NewLine + but,
                               subject);
                   };
        }
    }
}
